﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows;

namespace Rabbit.Utils
{
    public class LogHelper
    {
        static StringBuilder logInfo = new StringBuilder();
        private static object obj = new object();
        public static void LogError(Exception ex)
        {
            string logPath = Directory.GetCurrentDirectory() + "/log";
            if (!Directory.Exists(logPath))
            {
                Directory.CreateDirectory(logPath);
            }

            lock (obj)
            {
                logInfo.Clear();
                logInfo.Append(Environment.NewLine);
                logInfo.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                logInfo.Append("\t");
                logInfo.Append(ex.Message);
                logInfo.Append(ex.StackTrace);
                File.AppendAllText(logPath + "/log.txt", logInfo.ToString());
            }
        }

        public static void OpenLogFolder()
        {
            DirectoryInfo logDir = new DirectoryInfo(Directory.GetCurrentDirectory() + "/log");
            if (logDir.Exists)
            {
                try
                {
                    System.Diagnostics.Process.Start("explorer.exe", logDir.FullName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("打开日志文件夹失败，详细信息：" + ex.Message);
                }
            }
            else
            {
                Clipboard.SetText(Directory.GetCurrentDirectory());
                MessageBox.Show("暂无日志文件");
            }
        }
    }
}
